﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using log4net;
using System.Reflection;
using System.Web;
using System.Web.UI;

namespace AsyncWebPart
{
    public class BaseWebPart : WebPart
    {
        #region Fields
        protected Panel _contentContainer;

        // This declares a logger using the page's type to scope the logger correctly.
        protected static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        #region Public Properties
        protected String CurrentUserId
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.ID.ToString();
            }
        }
        #endregion

        #region Helper Methods
        /// <summary>
        /// Set up the content container for AJAX
        /// </summary>
        protected void PrepareContentContainer()
        {
            try
            {
                if (HttpContext.Current.Request.Browser.EcmaScriptVersion.Major >= 1)
                {
                    //Busy Box View (JS enabled)
                    Image spinnerImage = new Image();
                    spinnerImage.ImageUrl = String.Format("{0}{1}", SPContext.Current.Web.Url, Constants.SpinnerImagePath);
                    Label spinnerText = new Label() { Text = Constants.WaitMessage };

                    _contentContainer.ID = "" + this.ID + "Context";
                    _contentContainer.Controls.Add(spinnerImage);
                    _contentContainer.Controls.Add(new LiteralControl("<br />"));
                    _contentContainer.Controls.Add(spinnerText);
                    //AJAX Enabled
                }
                else
                {
                    //AJAX Disabled - Fallback mode

                    //Alternative content in case the user's browser does not support JS
                    _contentContainer.Controls.Add(new LiteralControl("It seems your browser doesn't support Javascript"));
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error in BaseWebPart.PrepareContentContainer", ex);
            }
        }

        /// <summary>
        /// Sets up an ASP.net Script Manager on the page, connects it to a specified web service, and registers JS handlers to go with it.
        /// </summary>
        protected void AddScriptManagerReferenceProxy(string referenceProxyUrl, string referenceProxyJS)
        {
            try
            {
                ScriptManager thisScriptManager = ScriptManager.GetCurrent(this.Page);
                if (thisScriptManager == null)
                {
                    thisScriptManager = new ScriptManager();
                    this.Controls.Add(thisScriptManager);
                }

                ServiceReference referenceProxy = new ServiceReference();
                referenceProxy.Path = referenceProxyUrl;

                referenceProxy.InlineScript = true;
                //If you are having issues identifying the names of the web service JS methods, uncomment the line above ^

                thisScriptManager.Services.Add(referenceProxy);

                //JS file contains proxy methods for web service - see file for details

                this.Page.ClientScript.RegisterClientScriptInclude(typeof(Page), referenceProxyUrl, referenceProxyJS);
            }
            catch (Exception ex)
            {
                Logger.Error("Error in BaseWebPart.AddScriptManagerReferenceProxy", ex);
            }
        }
        #endregion
    }
}
